package com.group7.mall.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * @Description: 新增时填充
     * @auther: zpq
     * @date: 2020/11/2 2:26 下午
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        // this.setFieldValByName(字段名, 内容, metaObject：用这个对象进行填充用的，称为元数据对象);
        System.out.println("o1232112312");
        metaObject.setValue("brand", "aljkhdajlhsa");
        this.setFieldValByName("brand", "hhhhhhh", metaObject);
        // ================setFieldValByName 方法内的内容，有兴趣可以进去看一看，中文注释的==================================================
        /// **
        // * 通用填充
        // *
        // * @param fieldName  java bean property name
        // * @param fieldVal   java bean property value
        // * @param metaObject meta object parameter
        // */
        // default MetaObjectHandler setFieldValByName(String fieldName, Object fieldVal, MetaObject
        // metaObject) {
        //  if (Objects.nonNull(fieldVal) && metaObject.hasSetter(fieldName)) {
        //    metaObject.setValue(fieldName, fieldVal);
        //  }
        //  return this;
        // }
    }

    /**
     * * @Description: 更新时填充
     *
     * @auther: zpq
     * @date: 2020/11/2 11:37 上午
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        System.out.println("okokokkokokok");
        this.setFieldValByName("updateBy", "admin", metaObject);
    }
}

//public class MyMetaObjectHandler implements MetaObjectHandler {
//    @Override
//    public void insertFill(MetaObject metaObject) {
////        this.setFieldValByName("createdTime", new Date(), metaObject);
////        this.setFieldValByName("updatedTime", new Date(), metaObject);
////        this.strictInsertFill(metaObject, "brand", String.class, "zuoshenge");
//        this.strictInsertFill(metaObject,"createdTime", Date.class, new Date());
//        this.strictInsertFill(metaObject, "isDeleted", Integer.class, 0);
////        this.setFieldValByName("created_time", new Date(), metaObject);
////        this.setFieldValByName("updated_time", new Date(), metaObject);
////        this.strictUpdateFill(metaObject,"updatedTime", Date.class, new Date());
////        this.strictInsertFill(metaObject, "version", Integer.class, 0);
//    }
//
//    @Override
//    public void updateFill(MetaObject metaObject) {
////        this.strictUpdateFill(metaObject, "updateBy", String.class, "zuoshenge");
////        this.strictUpdateFill(metaObject,"createdTime", Date.class, new Date());
//        this.strictUpdateFill(metaObject,"updatedTime", Date.class, new Date());
////        this.strictUpdateFill(metaObject,"createdTime", Date.class, new Date());
////        this.setFieldValByName("created_time", new Date(), metaObject);
////        this.setFieldValByName("updated_time", new Date(), metaObject);
////        this.setFieldValByName("createdTime", new Date(), metaObject);
////        this.setFieldValByName("updatedTime", new Date(), metaObject);
//    }
//}
